package condominio

import grails.converters.JSON

class PagosController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[pagosInstanceList: Pagos.list(params), pagosInstanceTotal: Pagos.count()]
	}

    def create = {
        def pagosInstance = new Pagos()
        pagosInstance.properties = params
        return [pagosInstance: pagosInstance]
    }

    def create1 = {
		def c11 =params["c1"] 
		
        def pagosInstance = new Pagos()
		
        pagosInstance.properties = params
		
        return [pagosInstance: pagosInstance, c2 : c11]
    }
	
    def save = {
        def pagosInstance = new Pagos(params)

                                def cuotasInstance = new Cuotas()

                                cuotasInstance = Cuotas.findById(pagosInstance.cuotas.id)

                                cuotasInstance.estado = 2

        if (pagosInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'pagos.label', default: 'Pagos'), pagosInstance.id])
            redirect(action: "show", id: pagosInstance.id)
        }
        else {
            render(view: "create", model: [pagosInstance: pagosInstance])
        }
    }

	
	def save1 = {
        def pagosInstance = new Pagos(params)

                                def cuotasInstance = new Cuotas()

                                cuotasInstance = Cuotas.findById(pagosInstance.cuotas.id)

                                cuotasInstance.estado = 2

        if (pagosInstance.save(flush: true)) {
            flash.message = message(code: 'default.created.message', args: [message(code: 'pagos.label', default: 'Pagos'), pagosInstance.id])
            redirect(controller:"cuotas", action: "list1", id: pagosInstance.id)
        }
        else {
            render(view: "create", model: [pagosInstance: pagosInstance])
        }
    }
	
	
    def show = {
        def pagosInstance = Pagos.get(params.id)
        if (!pagosInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pagos.label', default: 'Pagos'), params.id])
            redirect(action: "list")
        }
        else {
            [pagosInstance: pagosInstance]
        }
    }

    def edit = {
        def pagosInstance = Pagos.get(params.id)
        if (!pagosInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pagos.label', default: 'Pagos'), params.id])
            redirect(action: "list")
        }
        else {
            return [pagosInstance: pagosInstance]
        }
    }

    def update = {
        def pagosInstance = Pagos.get(params.id)
        if (pagosInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (pagosInstance.version > version) {
                    
                    pagosInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'pagos.label', default: 'Pagos')] as Object[], "Another user has updated this Pagos while you were editing")
                    render(view: "edit", model: [pagosInstance: pagosInstance])
                    return
                }
            }
            pagosInstance.properties = params
            if (!pagosInstance.hasErrors() && pagosInstance.save(flush: true)) {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'pagos.label', default: 'Pagos'), pagosInstance.id])
                redirect(action: "show", id: pagosInstance.id)
            }
            else {
                render(view: "edit", model: [pagosInstance: pagosInstance])
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pagos.label', default: 'Pagos'), params.id])
            redirect(action: "list")
        }
    }

    def delete = {
        def pagosInstance = Pagos.get(params.id)
		
		                        def cuotasInstance = new Cuotas()

                                cuotasInstance = Cuotas.findById(pagosInstance.cuotas.id)

                                cuotasInstance.estado = 0
		
		
        if (pagosInstance) {
            try {
                pagosInstance.delete(flush: true)
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'pagos.label', default: 'Pagos'), params.id])
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'pagos.label', default: 'Pagos'), params.id])
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'pagos.label', default: 'Pagos'), params.id])
            redirect(action: "list")
        }
    }
}
